package com.whitedust.top70;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;

/**
 *
 * @author A
 * @since 2025/11/11 12:55
 */

public class Top64 {
    public String decodeString(String s) {
        char[] cs = s.toCharArray();
        LinkedList<Integer>stackMulti = new LinkedList<>();
        LinkedList<String>stackRes = new LinkedList<>();
        int multi = 0;
        StringBuilder res = new StringBuilder();
        for (char c : cs) {
            if(c=='['){
                stackMulti.addLast(multi);
                stackRes.addLast(res.toString());
                multi = 0;
                res = new StringBuilder();
            } else if (c==']') {
                StringBuilder tmp = new StringBuilder();
                int curMulti = stackMulti.removeLast();
                for(int i = 0;i<curMulti;i++){
                    tmp.append(res);
                }
                res = new StringBuilder(stackRes.removeLast()+tmp);
            } else if (c>='0'&&c<='9') {
                multi = multi*10+Integer.parseInt(c+"");
            }else res.append(c);
        }
        return res.toString();
    }
}
